const baseUrl = 'http://localhost:4000'

function ajax({ method = 'GET', url, params = {}, data = {} } = {}) {
  return new Promise(async function (resolve, reject) {
    let res = await axios({
      method,
      url: baseUrl+ url,
      params,
      data,
      headers: {
        Authorization: `Bearer ${localStorage.token}`,
      },
    })

		let {code,msg,data: data1} = res.data;

		// 验证失败，重新登陆
		if(code == 401){
			location.href = '/page/login.html';
			return;
		}

		// 服务端错误
		if(code == 500){
			alert('服务端错误，请稍后重试！')
			return;
		}

		// 其他错误
		if(code){
			alert(msg)
			return;
		}

		// 请求成功 -> 返回数据

		resolve(data1)
  })
}
